/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.simba.utils.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 *
 * @author lejtovicz
 */
public class SentenceSplitter {

    public List<String> doSentenceSplitting(String inputText) {
        // public int doTokenizing() {
        int wordPos = 0;
        int wordCount = 0;
        int firstWordPos = -1;
        int lastWordPos = -1;
        int sentenceNum = 0;
        int sentencePos = 0;
        int sentenceStart = 0;
        List<String> sentenceData = new ArrayList<String>();
        String sentence = "";

        String[] nonWhitespaceTokens = inputText.split("\\s+");

        for (String nonWhitespaceToken : nonWhitespaceTokens) {
            if (!nonWhitespaceToken.isEmpty()) {
                List<String> wList = tokenizeByPatterns(nonWhitespaceToken);
                //logger.debug(wList);
                for (int i = 0; i <= wList.size() - 1; i++) {
                    String w = wList.get(i);
                    String nextWord = null;
                    if (i < wList.size() - 1) {
                        nextWord = wList.get(i + 1);
                    }
                    if (w == null) {
                        // ignore
                    } else if (!TokenizationPatterns.SZE2.contains(w)) { //the actual String is not a punctuation (ie. we are not at the end of a sentence)
                        if (sentencePos == 0) { // Satzanfang
                            sentenceNum++;
                            sentenceStart = wordPos;
                        }

                        if (TokenizationPatterns.wordPattern.matcher(w).matches() && (nextWord == null || (nextWord != null && !TokenizationPatterns.SZE2.contains(nextWord)))) { // (w ==~ WORD) if w is a word but it is not the last word of a sentence
                            sentence = sentence + " " + w;
                        } else if (TokenizationPatterns.wordPattern.matcher(w).matches() && nextWord != null && TokenizationPatterns.SZE2.contains(nextWord)) { // (w ==~ WORD) if w is a word and it is the last word of a sentence
                            sentence = sentence + " " + w;
                        } else {
                            // logger.debug("dropping '" + w + "'");
                        }
                        sentencePos++;
                        wordPos++;
                    } else { // Satzende (end of sentence)
                        firstWordPos = sentenceStart;
                        lastWordPos = wordPos - 1;
                        sentencePos = 0;
                        sentenceData.add(sentence);
                        sentence = "";
                    }
                } // for
            }
        } // for


        // falls offener Satz
        /*
        if (sentencePos>0) {
        sentenceData += [ 'firstWordPos':(sentenceStart) , 'lastWordPos':(wordPos-1) ]
        }
         */

        // jetzt steht Gesamtwortzahl fest
        wordCount = wordPos;
        return sentenceData;
    }

    public List<String> tokenizeByPatterns(String text) {
        List<String> before = new ArrayList<String>();
        List<String> here = new ArrayList<String>();
        List<String> after = new ArrayList<String>();

        // Satzzeichen am Anfang und Ende absplitten
        String firstChar = text.substring(0, 1);
        if (text.length() > 1 && TokenizationPatterns.SZ.contains(firstChar)) {
            before.add(firstChar);
            text = text.substring(1);
        }
        String lastChar = text.substring(text.length() - 1);
        if (text.length() > 1 && TokenizationPatterns.SZ.contains(lastChar)) {
            after.add(0, lastChar);
            text = text.substring(0, text.length() - 1);
        }

        Matcher m2 = TokenizationPatterns.m2Pattern.matcher(text);
        if (m2.matches()) {
            if (m2.groupCount() >= 1) {
                if (m2.group(1) != null) {
                    before.add(m2.group(1));
                }
                if (m2.groupCount() >= 2) {
                    if (m2.group(2) != null) {
                        before.add(m2.group(2));
                    }
                    if (m2.groupCount() >= 3) {
                        if (m2.group(3) != null) {
                            here.add(m2.group(3));
                        }
                        if (m2.groupCount() >= 4) {
                            if (m2.group(4) != null) {
                                after.add(0, m2.group(4));
                            }
                            if (m2.groupCount() == 5) {
                                if (m2.group(5) != null) {
                                    after.add(0, m2.group(5));
                                }
                            }
                        }
                    }
                }
            }
        } else {
            here.add(text);
        }

        here.addAll(0, before);
        here.addAll(after);
        return here;
    }
}
